package com.itheima.HomeWork_day14;
/*
生产者与消费者问题:

 */
public class TestDesk03 {
    public static void main(String[] args) {

        Desk desk=new Desk();

        //创建线程
        Foodie f=new Foodie(desk);
        f.start();
        Cooker c=new Cooker(desk);
        c.start();

    }
}

/*
该类用来控制线程
 */
class Desk {
    //设置线程的执行次数
   // public static int count=10;
    private  int count;

    //定义一个标记
    //public static boolean flag=true;
    private  boolean flag;
    //创建一个共同的锁对象
  //  public static final Object lock=new Object();
    private  final Object lock=new Object();

    public Desk() {
        this(10,false);
    }

    public Desk(int count, boolean flag) {
        this.count = count;
        this.flag = flag;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public Object getLock() {
        return lock;
    }

    @Override
    public String toString() {
        return "Desk{" +
                "count=" + count +
                ", flag=" + flag +
                ", lock=" + lock +
                '}';
    }
}


class Foodie extends Thread {
    private Desk desk;
    public Foodie(Desk desk) {
        this.desk=desk;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (desk.getLock()) {
                if (desk.getCount() == 0) {
                    break;
                } else {
                    if (desk.isFlag()) {
                        //消费
                        System.out.println("吃货正在吃...");
                        desk.setFlag(false);
                        desk.setCount(desk.getCount()-1);
                        //唤醒
                       desk.getLock().notifyAll();
                    } else {
                        //否则就等待
                        try {
                           desk.getLock().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }
    }
}

class Cooker extends Thread{
    private Desk desk;

    public Cooker(Desk desk) {
        this.desk=desk;
    }

    @Override
    public void run() {
        while(true){
            synchronized (desk.getLock()){
                if (desk.getCount()==0){
                    break;
                }else{
                    if (!desk.isFlag()){
                        //生产
                        System.out.println("厨师正在生产...");
                        desk.setFlag(true);

                        //唤醒
                       desk.getLock().notifyAll();
                    }else{
                        //否则就等待
                        try {
                            desk.getLock().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }
    }
}